package com.zidian.leader.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.orhanobut.logger.Logger;
import com.zidian.leader.R;
import com.zidian.leader.adapter.QuestionnaireChartListAdapter;
import com.zidian.leader.api.Constant;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.MySwipeBackActivity;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.QuestionnaireDetail;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 就业质量Activity
 * Created by GongCheng on 2016/10/24 0024.
 */

public class EmploymentQualityActivity extends MySwipeBackActivity {
    @Bind(R.id.employment_eva_list_rv)
    RecyclerView employmentEvaListRv;

    private QuestionnaireChartListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_employment_evaluation;
    }

    private void initView() {
        //Toolbar
        actionBar.setTitle(R.string.employment_quality);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);
        adapter = new QuestionnaireChartListAdapter(this);
        employmentEvaListRv.setLayoutManager(new LinearLayoutManager(this));
        employmentEvaListRv.setAdapter(adapter);
        getQuestionnaireDetail();
    }

    public void getQuestionnaireDetail() {
        subscription = HttpFactory.getInstance().getQuestionnaireDetail(HawkUtils.getUsername(),
                Constant.QuestionnaireType.EMPLOYMENT_QUALITY,
                HawkUtils.getRoleId() + StringConstants.STRING_EMPTY, HawkUtils.getToken())
                .map(new Func1<List<QuestionnaireDetail>, List<QuestionnaireDetail>>() {
                    @Override
                    public List<QuestionnaireDetail> call(List<QuestionnaireDetail> questionnaireDetails) {
                        //对问题进行排序
                        Collections.sort(questionnaireDetails, new Comparator<QuestionnaireDetail>() {
                            @Override
                            public int compare(QuestionnaireDetail o1, QuestionnaireDetail o2) {
                                return o1.getQuestionNum() - o2.getQuestionNum();
                            }
                        });
                        return questionnaireDetails;
                    }
                })
                .subscribe(new Subscriber<List<QuestionnaireDetail>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        Logger.e(e.toString());
                        Snackbar.make(employmentEvaListRv, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<QuestionnaireDetail> questionnaireDetails) {
                        adapter.setData(questionnaireDetails);
                    }
                });
    }


}
